package com.hongfeng.support.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hongfeng.support.annotation.Online;
import com.hongfeng.support.common.Page;
import com.hongfeng.support.controller.common.BaseController;
import com.hongfeng.support.item.ParmItem;
import com.hongfeng.support.item.tag.FieldOperator;
import com.hongfeng.support.model.Role;
import com.hongfeng.support.service.RoleService;
import com.hongfeng.support.util.json.JsonUtils;

/**
 * 角色Controller
 * 
 * @author llc
 * 
 */
@Controller
@Online
public class RoleController extends BaseController {

	@Resource
	private RoleService roleService;

	@RequestMapping("list")
	public void list(HttpServletRequest request, HttpServletResponse response,
			String roleName, Page<Role> page) {
		log.info("查询角色名：" + roleName);
		ParmItem item = new ParmItem("roleName", roleName, FieldOperator.LIKE);
		roleService.findPageByQuery(page, Role.class, item);
		String json = JsonUtils.bean2Json(page);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("listAll")
	public void listAll(HttpServletRequest request, HttpServletResponse response) {
		List<Role> roles = roleService.findAll();
		String json = JsonUtils.array2Json(roles, "memo");
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("create")
	public void create(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("role") String roleJson) {
		log.info("添加角色:" + (roleJson != null ? roleJson : null));
		if (roleJson == null) {
			this.sendErrorResponse(response, "角色信息不能为空。");
			return;
		}
		Role role = (Role) JsonUtils.json2Object(roleJson, Role.class);
		ParmItem item = new ParmItem("roleName", role.getRoleName(),
				FieldOperator.EQUAL);
		List<Role> roles = roleService.findAllByQuery(Role.class, item);
		if (roles.size() == 0) {
			roleService.create(role);
		} else {
			this.sendErrorResponse(response, "角色名已经存在。");
		}
	}

	@RequestMapping("update")
	public void update(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("role") String roleJson) {
		log.info("修改角色:" + (roleJson != null ? roleJson : null));
		if (roleJson == null) {
			this.sendErrorResponse(response, "角色信息不能为空。");
			return;
		}
		Role role = (Role) JsonUtils.json2Object(roleJson, Role.class);
		Role qRole = roleService.read(role.getId());
		List<Role> roles = null;
		if (!role.getRoleName().equals(qRole.getRoleName())) {
			ParmItem item = new ParmItem("roleName", role.getRoleName(),
					FieldOperator.EQUAL);
			roles = roleService.findAllByQuery(Role.class, item);
		}
		if (roles == null || roles.size() == 0) {
			role.setCreateTime(qRole.getCreateTime());
			role.setCreator(qRole.getCreator());
			roleService.update(role);
		} else {
			this.sendErrorResponse(response, "角色名已经存在。");
		}
	}

	@RequestMapping("read")
	public void read(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("id") Long id) throws Exception {
		log.info("读取角色:" + id);
		Role role = roleService.read(id);
		String json = JsonUtils.bean2Json(role);
		log.info("角色信息：" + json);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("ids") String ids) {
		log.info("删除角色:" + (ids != null ? ids.toString() : null));
		if (ids == null || "".equals(ids)) {
			this.sendErrorResponse(response, "请选择要删除的角色。");
			return;
		}
		try {
			roleService.deleteRoleWithRelation(ids);
		} catch (Exception e) {
			e.printStackTrace();
			this.sendErrorResponse(response, "删除角色信息失败。");
		}
	}

}